local extension = Package("lzsj1")
extension.extensionName = "lzsj"

local U = require "packages/utility/utility"
local DIY = require "packages/diy_utility/diy_utility"
local ls = require 'packages/lvshisha/utility/lvshi_util'
local TT = require "packages/tuntian_studio/utility"

Fk:loadTranslationTable{
  ["lzsj1"] = "雷组-全史",
}

local liuyu = General(extension, "lzsj__liuyu", "jin", 4)
liuyu.strategy = "schemecourage"

Fk:loadTranslationTable {
  [liuyu.name] = "刘裕",
  ["#"..liuyu.name] = "腾鳞升汉",
  ["illustrator:"..liuyu.name] = "官方",
  ["designer:"..liuyu.name] = "Erosa",
}

local shengbo = fk.CreateViewAsSkill {
  name = "lzsj__shengbo",
  pattern = "slash",
  card_filter = Util.FalseFunc,
  view_as = function (self, cards)
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    return slash
  end,
  before_use = function (self, player, use)
    local room = player.room
    use.extraUse = true
    for _, pid in ipairs(use.tos[1]) do
      local target = room:getPlayerById(pid)
      local hands = player:getCardIds("h")
      local to_show = room:askForCardsChosen(target, player, 0, #hands, "h", self.name, "shengbo-show:"..player.id)
      if #to_show > 0 then
        player:showCards(to_show)
      end

      local additional = #table.filter(to_show, function (cid)
        return Fk:getCardById(cid).trueName == "slash"
      end)
      use.card.extra_data = use.card.extra_data or {}
      use.card.extra_data.shengbo = additional or 1
    end
  end,
  enabled_at_play = function (self, player)
    return not player:isKongcheng()
  end,
  enabled_at_response = function (self, player, response)
    return not player:isKongcheng() and not response
  end,
}

local shengbo_delay = fk.CreateTriggerSkill {
  name = "#lzsj__shengbo_delay",
  mute = true,
  events = {fk.CardUseFinished, fk.Damage, fk.DamageCaused},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(shengbo) or player ~= target then return end
    if event == fk.CardUseFinished then
      return table.contains(data.card.skillNames, shengbo.name) and not data.damageDealt
    elseif event == fk.Damage then
      return data.card and data.card.trueName == "slash" and data.card:isVirtual()
    elseif event == fk.DamageCaused then
      return data.card and table.contains(data.card.skillNames, shengbo.name) and data.card.extra_data.shengbo
    end
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      local tos = U.getActualUseTargets(room, data, event)
      local to_dis = room:askForDiscard(player, 1, 999, false, shengbo.name, true, "slash", "shengbo-discard")
      if #to_dis > 0 then
        for _, pid in ipairs(tos) do
          local suffer = room:getPlayerById(pid)
          if not suffer.dead then
              room:damage {
              damage = #to_dis,
              to = suffer,
              from = player,
              skillName = shengbo.name,
            }
          end
        end
      else
        room.current._phase_end = true
        for _, pid in ipairs(tos) do
          local suffer = room:getPlayerById(pid)
          if not suffer:isKongcheng() then
            if player.dead then break end
            local to_prey = room:askForCardChosen(player, suffer, "h", shengbo.name)
            room:obtainCard(player, to_prey, false, fk.ReasonPrey, player.id, shengbo.name)
          end
        end
      end
    elseif event == fk.Damage then
      local to_recast = table.filter(player:getCardIds("h"), function (cid)
        return Fk:getCardById(cid).trueName == "slash"
      end)
      if #to_recast > 0 then
        room:recastCard(to_recast, player, shengbo.name)
      end
    elseif event == fk.DamageCaused then
      data.damage = data.card.extra_data.shengbo
    end
  end
}
shengbo:addRelatedSkill(shengbo_delay)

local shengbo_mode = fk.CreateTargetModSkill {
  name = "#lzsj__shengbo_mode",
  bypass_times = function(self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, shengbo.name)
  end,
}
shengbo:addRelatedSkill(shengbo_mode)

liuyu:addSkill(shengbo)

Fk:loadTranslationTable {
  [shengbo.name] = "盛搏",
  [":"..shengbo.name] = "当你需要使用【杀】时，若你有手牌，你可视为使用之（不计入次数限制），目标能展示你任意张手牌并将其伤害改为以此法展示的【杀】数。"
  .."若未造成伤害，你须选择：弃置至少一张【杀】并对目标造成等量伤害，或结束此阶段并获得目标一张手牌。"
  .."额外。你以虚拟【杀】造成伤害后，你重铸所有【杀】。",

  ["shengbo-show"] = "盛搏：请展示 %src 的任意张手牌，将其对你使用的【杀】的伤害改为展示的【杀】数量",

  [shengbo_delay.name] = "盛搏",
  ["shengbo-discard"] = "盛搏：请弃置任意张【杀】，对目标造成等量伤害；或点取消结束本阶段，获得目标各一张手牌",
}

local gouxi = General(extension, "lzsj__gouxi", "jin", 4, 5)
gouxi.strategy = "courage"

Fk:loadTranslationTable{
  ["lzsj__gouxi"] = "苟晞",
  ["#lzsj__gouxi"] = "苟道何穷",
  ["designer:lzsj__gouxi"] = "Koishi",
  ["illustrator:lzsj__gouxi"] = "珊瑚鑫",
}

local hezhen = fk.CreateTriggerSkill {
  name = "lzsj__hezhen",
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {"lzsj__hezhen_damage", "lzsj__hezhen_draw", "lzsj__hezhen_lianpo"}
    local choices = table.simpleClone(all_choices)
    local record = player:getTableMark("lzsj__hezhen_record")
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(record, p.id)
    end)
    if #targets > 0 then
      for _, pid in ipairs(record) do
        room:setPlayerMark(room:getPlayerById(pid), "@@lzsj__hezhen", 1)
      end
    else
      table.remove(choices, 1)
    end
    local choice = room:askForChoice(player, choices, self.name, nil, false, all_choices)
    for _, pid in ipairs(record) do
      room:setPlayerMark(room:getPlayerById(pid), "@@lzsj__hezhen", 0)
    end
    if choice == "lzsj__hezhen_damage" then
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lzsj__hezhen-choose", self.name, false)
      if #tos > 0 then
        room:damage { from = player, to = room:getPlayerById(tos[1]), damage = 1, skillName = self.name }
      end
    elseif choice == "lzsj__hezhen_draw" then
      room:changeMaxHp(player, -1)
      if not player.dead then
        player:drawCards(2, self.name)
      end
    else
      room:handleAddLoseSkills(player, "-lzsj__hezhen|lzsj__lianpo")
    end
  end,

  refresh_events = {fk.Damage},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:addTableMarkIfNeed(player, "lzsj__hezhen_record", data.to.id)
  end,
}
gouxi:addSkill(hezhen)

local lianpo = fk.CreateTriggerSkill{
  name = "lzsj__lianpo",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      local events = room.logic:getEventsOfScope(GameEvent.Death, 1, function(e)
        local deathStruct = e.data[1]
        return deathStruct.damage and deathStruct.damage.from and deathStruct.damage.from == player
      end, Player.HistoryTurn)
      return #events > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lzsj__lianpo-invoke")
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraTurn(true)
  end,
}
gouxi:addRelatedSkill(lianpo)
Fk:loadTranslationTable{
  ["lzsj__lianpo"] = "连破",
  [":lzsj__lianpo"] = "当你杀死一名角色后，你可于此回合结束后获得一个额外回合。",
  ["#lzsj__lianpo-invoke"] = "连破：你可以额外执行一个回合！",
}

Fk:loadTranslationTable{
  ["lzsj__hezhen"] = "赫震",
  [":lzsj__hezhen"] = "锁定技，出牌阶段开始时，你执行以下一项:对一名未受到过你伤害的角色造成1点伤害，减1点体力上限并摸两张牌，失去此技能并获得“连破” 。",
  ["lzsj__hezhen_damage"] = "对一名未受到过你伤害的角色造成一点伤害",
  ["lzsj__hezhen_draw"] = "减一点体力上限并摸两张牌",
  ["lzsj__hezhen_lianpo"] = "失去“赫震”并获得“连破” ",
  ["#lzsj__hezhen-choose"] = "赫震：对一名未受到过你伤害的角色造成一点伤害",
  ["@@lzsj__hezhen"] = "造成过伤害",
}

local weiting = fk.CreateViewAsSkill{
  name = "lzsj__weiting",
  pattern = ".",
  prompt = "#lzsj__weiting",
  interaction = function(self)
    local choices = {}
    if Self:getMark("lzsj__weiting_damage-phase") > 0 then
      choices = U.getViewAsCardNames(Self, self.name, U.getAllCardNames("b"))
    end
    if Self:getMark("lzsj__weiting_recover-phase") > 0 then
      table.insertTable(choices, U.getViewAsCardNames(Self, self.name, U.getAllCardNames("t")))
    end
    if Self:getMark("lzsj__weiting_getcard-phase") > 0 then
      local equips = {}
      for _, id in ipairs(Fk:currentRoom().discard_pile) do
        local card = Fk:getCardById(id)
        if card.type == Card.TypeEquip and Self:canUse(card) and not Self:prohibitUse(card) then
          table.insertIfNeed(equips, card.name)
        end
      end
      table.insertTable(choices, equips)
    end
    if #choices > 0 then
      return U.CardNameBox { choices = choices }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local name = self.interaction.data
    if name == nil then return nil end
    local c = Fk:cloneCard(name)
    if c.type == Card.TypeEquip then
      local cid = table.find(Fk:currentRoom().discard_pile, function (id)
        local card = Fk:getCardById(id)
        return card.name == name and Self:canUse(card) and not Self:prohibitUse(card)
      end)
      if not cid then return nil end
      return Fk:getCardById(cid)
    end
    c.skillName = self.name
    return c
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and
    (player:getMark("lzsj__weiting_damage-phase") > 0 or player:getMark("lzsj__weiting_recover-phase") > 0
    or player:getMark("lzsj__weiting_getcard-phase") > 0)
  end,
  enabled_at_response = Util.FalseFunc,
}

local weiting_trigger = fk.CreateTriggerSkill {
  name = "#lzsj__weiting_trigger",
  anim_type = "negative",
  events = {fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    return target == player and not player.dead and player:usedSkillTimes(weiting.name, Player.HistoryPhase) > 0
    and (player:getMark("lzsj__weiting_damage-phase") == 0 or player:getMark("lzsj__weiting_recover-phase") == 0
    or player:getMark("lzsj__weiting_getcard-phase") == 0)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = 0
    for _, name in ipairs({"damage", "recover", "getcard"}) do
      if player:getMark("lzsj__weiting_"..name.."-phase") == 0 then
        num = num + 1
      end
    end
    room:loseHp(player, num, weiting.name)
  end,

  refresh_events = {fk.Damage, fk.HpRecover, fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if not player:hasSkill(weiting, true) then return end
    if event == fk.Damage then
      return target == player and player.phase == Player.Play and player:getMark("lzsj__weiting_damage-phase") == 0
    elseif event == fk.HpRecover then
      return target == player and player.phase == Player.Play and player:getMark("lzsj__weiting_recover-phase") == 0
    elseif player.phase == Player.Play and player:getMark("lzsj__weiting_getcard-phase") == 0 then
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Player.Hand then
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:setPlayerMark(player, "lzsj__weiting_damage-phase", 1)
    elseif event == fk.HpRecover then
      room:setPlayerMark(player, "lzsj__weiting_recover-phase", 1)
    else
      room:setPlayerMark(player, "lzsj__weiting_getcard-phase", 1)
    end
  end,
}
weiting:addRelatedSkill(weiting_trigger)
gouxi:addSkill(weiting)

Fk:loadTranslationTable{
  ["lzsj__weiting"] = "危庭",
  [":lzsj__weiting"] = "阶段技，若你已于此阶段内造成过伤害/回复过体力/获得过牌，你可(视为)使用一张基本/通常锦囊/弃牌堆内的装备牌，若如此做，此阶段结束时，你失去未完成项数的体力。",
  ["#lzsj__weiting_trigger"] = "危庭",
  ["#lzsj__weiting"] = "危庭：选择你要（视为）使用的牌名",
}

local murongchui = General(extension, "lzsj__murongchui", "yan", 4)
murongchui.strategy = "schemecourage"
murongchui.guo16 = true

Fk:loadTranslationTable {
  [murongchui.name] = "慕容垂",
  ["#"..murongchui.name] = "军步抵天",
  ["designer:"..murongchui.name] = "Erosa",
}

local bishen = fk.CreateTriggerSkill {
  name = "lzsj__bishen",
  anim_type = "support",
  events = {fk.TargetConfirming, fk.DamageCaused},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.TargetConfirming then
      return target == player and data.card.type == Card.TypeTrick
    else
      return not player:isKongcheng() and data.to == player and data.card and data.card.trueName == "slash"
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TargetConfirming then
      local from = room:getPlayerById(data.from)
      return room:askForSkillInvoke(from, self.name, data, "bishen-recover:"..player.id.."::"..data.card:toLogString())
    else
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TargetConfirming then
      AimGroup:cancelTarget(data, player.id)
      --room:askForDiscard(player, 1, player:getHandcardNum(), false, self.name, false)
      if not player.dead and player:isWounded() then
        room:recover {
          who = player,
          num = 1,
          skillName = self.name,
          recoverBy = room:getPlayerById(data.from),
        }
      end
    else
      room:broadcastPlaySound("./packages/standard_cards/audio/card/ice_sword")
      room:setEmotion(target, "./packages/standard_cards/image/anim/ice_sword")
      Fk.skills["#ice_sword_skill"]:use(event, target, target, data)
      return true
    end
  end,
}

murongchui:addSkill(bishen)

Fk:loadTranslationTable {
  [bishen.name] = "庇身",
  [":"..bishen.name] = "当你受到【杀】的伤害时，若你有手牌，来源须发动【寒冰剑】效果；当你成为锦囊牌的目标时，来源可取消之，令你回复1点体力。",
  ["bishen-recover"] = "庇身：是否令取消 %src 为 %arg 的目标并令其回复1点体力？",
}

local fenlve_marks = {}
fenlve_marks.extraTurn = "@@fenlve_extraTurn"
fenlve_marks.specialTurn = "fenlve_specialTurn"
fenlve_marks.real = "fenlve_realMark-turn"
fenlve_marks.perform = "@fenlve_performance-turn"

local setPerformanceMark = function (player)
  local perform_mark = "%s %s"
  local real = player:getTableMark(fenlve_marks.real)
  local yest = real.yest and '<font color="#E8251E"><b>昔</b></font>' or "昔"
  local now = real.now and '<font color="#E8251E"><b>今</b></font>' or "今"
  player.room:setPlayerMark(player, fenlve_marks.perform, string.format(perform_mark, yest, now))
end

local fenlve = fk.CreateTriggerSkill {
  name = "lzsj__fenlve",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove, fk.AfterTurnEnd},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.AfterCardsMove and player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    elseif event == fk.AfterTurnEnd then
      return player:getMark(fenlve_marks.extraTurn) == 1
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      return room:askForSkillInvoke(player, self.name, data)
    else
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      player:drawCards(player.hp, self.name)
      room:setPlayerMark(player, fenlve_marks.extraTurn, 1)
    else
      room:setPlayerMark(player, fenlve_marks.extraTurn, 0)
      room:setPlayerMark(player, fenlve_marks.specialTurn, 1)
      player:gainAnExtraTurn(true, self.name)
    end
  end,
}

local fenlve_speciel_turn = fk.CreateTriggerSkill { -- 特殊回合
  name = "#lzsj__fenlve_speciel_turn",
  mute = true,
  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return player == target and player:hasSkill(fenlve, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room.logic:getCurrentEvent():findParent(GameEvent.Turn, true, Player.HistoryGame):addCleaner( -- 回合结束清理标记
      function ()
        room:setPlayerMark(player, fenlve_marks.specialTurn, 0)
      end
    )
    local yest_turn = false
    local turns = room.logic:getEventsByRule(GameEvent.Turn, 2, function (e) return true end, Player.HistoryGame)
    if #turns > 1 then
      local start_index = turns[1].id
      local end_index = turns[2].id
      for i = start_index, end_index, -1 do
        local move_event = room.logic.all_game_events[i]
        if move_event.event == GameEvent.MoveCards then
          for _, move in ipairs(move_event.data) do
            if move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId).trueName == "slash" then
                  yest_turn = true
                  break
                end
              end
            end
          end
        end
      end
    end
    local mark = { yest = yest_turn, now = false }
    room:setPlayerMark(player, fenlve_marks.real, mark)
    setPerformanceMark(player)
  end,
}
fenlve:addRelatedSkill(fenlve_speciel_turn)

local fenlve_delay = fk.CreateTriggerSkill {
  name = "#lzsj__fenlve_delay",
  mute = true,
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if player.room.current and player.room.current ~= player then return end 
    if player:getTableMark(fenlve_marks.real).now or not player:hasSkill(fenlve, true) then return end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).trueName == "slash" then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(fenlve_marks.real)
    mark.now = true
    room:setPlayerMark(player, fenlve_marks.real, mark)
    setPerformanceMark(player)
  end,
}

fenlve:addRelatedSkill(fenlve_delay)

local fenlve_mod = fk.CreateTargetModSkill {
  name = "#lzsj__fenlve_mod",
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill(fenlve, true) and skill.trueName == "slash_skill"
    and scope == Player.HistoryPhase and player:getTableMark(fenlve_marks.real).yest then
      return 1
    end
  end,
  distance_limit_func = function(self, player, skill, card)
    if skill.trueName == "slash_skill" and player:getTableMark(fenlve_marks.real).now then
      return 999
    end
    return 0
  end,
}
fenlve:addRelatedSkill(fenlve_mod)

Fk:loadTranslationTable {
  [fenlve.name] = "奋略",
  [":"..fenlve.name] = "若当前回合/上个回合内有【杀】进入过弃牌堆，你使用【杀】无视距离/次数上限+1。轮次技，当你失去最后的手牌后，可将手牌数补至体力值，然后于此回合结束后进行一个额外回合。",
  [fenlve_marks.perform] = "奋略",
  [fenlve_marks.extraTurn] = "奋略 额外回合",
}

local xingba = fk.CreateTriggerSkill{
  name = "lzsj__xingba$",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and table.find(player.room:getOtherPlayers(player), function (p)
      return Fk.generals[p.general].guo16
    end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if Fk.generals[p.general].guo16 then
        local cards = room:askForCard(p, 1, 1, true, self.name, true, ".", "#lzsj__xingba-invoke:"..player.id, self.name)
        if #cards > 0 then
          room:obtainCard(player, cards, true, fk.ReasonGive, player.id, self.name)
          p:drawCards(1, self.name) 
        end
      end
    end
  end,
}

murongchui:addSkill(fenlve)
murongchui:addSkill(xingba)

Fk:loadTranslationTable {
  ["lzsj__xingba"] = "兴霸",
  [":lzsj__xingba"] = "主公技，回合开始时，其他16国角色可以依次交给你一张牌并摸一张牌。",
  ["#lzsj__xingba-invoke"] = "你可以交给%src一张牌并摸一张牌。",

  ["$lzsj__bishen1"] = "慕容霸已死，唯存垂首苻坚帐。",
  ["$lzsj__bishen2"] = "断齿藏锋，他日必裂长安城。",
  ["$lzsj__fenlve1"] = "淝水狼烟散，邺城燕旗扬！",
  ["$lzsj__fenlve2"] = "邺城旧旗裂，太行新锋出！",
  ["$lzsj__xingba1"] = "枋头雪耻血未冷，中山城头树燕旗！",
  ["$lzsj__xingba2"] = "七十白发披战甲，方显大燕兴本色！",
  ["~lzsj__murongchui"] = "参合陂上月，空照断槊魂...",
}

local xichao = General(extension, "lzsj__xichao", "jin", 3)
xichao.strategy = "scheme"
Fk:loadTranslationTable{
  ["lzsj__xichao"] = "郗超",
  ["#lzsj__xichao"] = "嘉幕映宾",
  ["designer:lzsj__xichao"] = "Koishi",
  ["illustrator:lzsj__xichao"] = "",
}

local juezuo = fk.CreateActiveSkill{
  name = "lzsj__juezuo",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#lzsj__juezuo",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    if #selected > 0 then return false end
    return to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local cards = to:getCardIds("h")
    if #cards > 0 then
      U.viewCards(player, cards, self.name, "$ViewCardsFrom:"..to.id)
    end
    if not player:isNude() then
      cards = room:askForCard(player, 1, 2, true, self.name, true, nil, "#lzsj__juezuo-give:"..to.id)
      if #cards > 0 then
        room:obtainCard(to, cards, false, fk.ReasonGive, player.id, self.name)
      end
      cards = to:getCardIds("h")
    end
    if to.dead then return end
    local maxNum, numMap, suits = 0, {}, {}
    for _, id in ipairs(cards) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        numMap[suit] = (numMap[suit] or 0) + 1
      end
    end
    for suit, num in pairs(numMap) do
      maxNum = math.max(maxNum, num)
    end
    if maxNum == 0 then return end
    for suit, num in pairs(numMap) do
      if num == maxNum then
        table.insert(suits, suit)
      end
    end
    local nameMap = {
      [Card.Club] = "slash",
      [Card.Heart] = "peach",
      [Card.Diamond] = "foresight",
      [Card.Spade] = "chasing_near",
    }
    local names = table.map(suits, function(s) return nameMap[s] end)
    while #names > 0 and not to.dead do
      local use = U.askForUseVirtualCard(room, to, names, nil, self.name, nil, false, true, false, true, nil, true)
      if not use then break end
      table.removeOne(names, use.card.name)
      room:useCard(use)
    end
  end,
}
xichao:addSkill(juezuo)

Fk:loadTranslationTable{
  ["lzsj__juezuo"] = "绝佐",
  [":lzsj__juezuo"] = "阶段技，出牌阶段，你可以观看一名其他角色的手牌并可交给其至多两张牌，然后若其手牌中梅花/红桃/方片/黑桃牌数最多，其视为使用【杀】/【桃】/【洞烛先机】/【逐近弃远】。",
  ["#lzsj__juezuo"] = "绝佐:观看一名角色手牌，交给其至多两张牌，再根据其手牌最多的花色，令其视为使用对应牌",
  ["#lzsj__juezuo-give"] = "绝佐：你可以交给 %src 至多两张牌",
}

local kuangdu = fk.CreateTriggerSkill{
  name = "lzsj__kuangdu",
  events = {fk.AskForCardUse, fk.AskForCardResponse},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and
      (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none"))) then
      if table.find(player.room.alive_players, function(p) return #p:getCardIds("ej") > 0 end) then
        local card = Fk:cloneCard("jink")
        card.skillName = self.name
        if event == fk.AskForCardUse then
          return not player:prohibitUse(card)
        else
          return not player:prohibitResponse(card)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local tos = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper), 1, 1,
    "#lzsj__kuangdu-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local targets = table.filter(room.alive_players, function (p) return #p:getCardIds("ej") > 0 end)
    if #targets == 0 then return false end
    local from = room:getPlayerById(room:askForChoosePlayers(to, table.map(targets, Util.IdMapper), 1, 1,
    "#lzsj__kuangdu-get:"..player.id, self.name, false)[1])
    local cid = room:askForCardChosen(to, from, "ej", self.name)
    room:obtainCard(to, cid, true, fk.ReasonPrey, player.id, self.name)

    if from == player then
      local card = Fk:cloneCard("jink")
      card.skillName = self.name
      if event == fk.AskForCardUse then
        if player:prohibitUse(card) then return false end
        data.result = {
          from = player.id,
          card = card,
        }
        if data.eventData then
          data.result.toCard = data.eventData.toCard
          data.result.responseToEvent = data.eventData.responseToEvent
        end
      else
        if player:prohibitResponse(card) then return false end
        data.result = card
      end
      return true
    end
  end
}
xichao:addSkill(kuangdu)

Fk:loadTranslationTable{
  ["lzsj__kuangdu"] = "旷度",
  [":lzsj__kuangdu"] = "你需要使用或打出【闪】时，你可以令一名其他角色获得场上一张牌，若为你场上，视为你使用或打出【闪】。",
  ["#lzsj__kuangdu-choose"] = "旷度：你可以令一名其他角色获得场上一张牌，若为你场上，你视为出【闪】",
  ["#lzsj__kuangdu-get"] = "旷度：请获得场上一张牌，若为 %src 的牌，其视为出【闪】",
}

local simayan = General(extension, "lzsj__simayan", "jin", 4)
simayan.strategy = "schemecourage"

local lzsj__dingge = fk.CreateTriggerSkill{
  name = "lzsj__dingge",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(U.getUniversalCards(room, "bt", false), function (id)
      return not table.contains(player:getTableMark("@$lzsj__dingge"), Fk:getCardById(id).name)
    end)
    --[[if #player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
      return e.data[1] == player
    end, end_id) == 0 then]]
    if player:getMark("lzsj__dingge-turn") == 0 then
      if room:getBanner("@$CenterArea") == nil then return end
      local ids = room:getBanner("@$CenterArea")
      local names = table.map(ids, function (id) return Fk:getCardById(id).name end)
      local centers = table.filter(U.getUniversalCards(room, "bt", false), function (id)
        return table.contains(names, Fk:getCardById(id).name)
      end)
      cards = table.filter(centers, function (id)
        return not table.contains(player:getTableMark("@$lzsj__dingge"), Fk:getCardById(id).name) end)
    end
    if #cards > 0 then
      local use = U.askForUseRealCard(room, player, cards, nil, self.name, "#dingge-use",
        {expand_pile = cards, bypass_times = true}, true, true)
      if use then
        self.cost_data = use
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local use = {
      card = Fk:cloneCard(self.cost_data.card.name),
      from = player.id,
      tos = self.cost_data.tos,
      extraUse = true,
    }
    use.card.skillName = self.name
    player.room:useCard(use)
  end,

  refresh_events = {fk.AfterCardUseDeclared, fk.HpChanged},--FIXME:记录器抽风，先refresh智慧
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      if event == fk.AfterCardUseDeclared then
        return data.card.type == Card.TypeBasic or data.card:isCommonTrick()
      else
        return data.n ~= 0
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      local names = player:getTableMark("@$lzsj__dingge")
      table.insertIfNeed(names, data.card.name)
      player.room:setPlayerMark(player, "@$lzsj__dingge", names)
    else
      player.room:addPlayerMark(player, "lzsj__dingge-turn")
    end
  end
}

local lzsj__huifa = fk.CreateTriggerSkill{
  name = "lzsj__huifa",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".",
    }
    room:judge(judge)
    local mark = player:getTableMark("@$lzsj__dingge")
    local name = ls.trueName(judge.card)
    if table.contains(mark, name) then
      table.removeOne(mark, name)
      if name == "lvshi__slash" then
        table.removeOne(mark, "lvs_fire__slash")
        table.removeOne(mark, "lvs_thunder__slash")
        table.removeOne(mark, "lvs_ice__slash")
      end
      room:setPlayerMark(player, "@$lzsj__dingge", mark)
      return true
    else
      room:moveCardTo(judge.card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
      player:setSkillUseHistory(self.name, 0, Player.HistoryRound)
    end
  end,
}

local lzsj__didu = fk.CreateTriggerSkill{
  name = "lzsj__didu$",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target.kingdom == "jin" then
      local n = 0
      for _, v in pairs(target.cardUsedHistory) do
        if v[Player.HistoryTurn] > 0 then
          n = n + v[Player.HistoryTurn]
          if n > 1 then return end
        end
      end
      return n == 1
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = ls.trueName(data.card)
    local mark = player:getTableMark("@$lzsj__dingge")
    if table.contains(mark, name) then
      table.removeOne(mark, name)
    else
      table.insert(mark, name)
    end
    room:setPlayerMark(player, "@$lzsj__dingge", mark)
  end,
}

lzsj__dingge.CenterArea = true
simayan:addSkill(lzsj__dingge)
simayan:addSkill(lzsj__huifa)
simayan:addSkill(lzsj__didu)

Fk:loadTranslationTable{
  ["lzsj__simayan"] = "司马炎",
  ["#lzsj__simayan"] = "康熙若变",
  ["designer:lzsj__simayan"] = "Koishi",
  ["illustrator:lzsj__simayan"] = "",

  ["lzsj__dingge"] = "鼎革",
  ["@$lzsj__dingge"] = "鼎革",
  [":lzsj__dingge"] = "轮次技，一名角色的回合结束时，若你的体力值于此回合变化过，则你可视为使用一张你未使用过的即时类牌，否则，你可视为使用中央区内一张你未使用过的即时类牌。",
  ["#dingge-use"] = "鼎革：请选择视为使用的牌",
  ["lzsj__huifa"] = "恢阀",
  [":lzsj__huifa"] = "轮次技，当你受到伤害时，你可进行一次判定，若为你使用过的牌名，则你防止此伤害并视为未使用过此牌，否则，你获得判定牌并重置此技能。",
  ["lzsj__didu"] = "帝度",
  [":lzsj__didu"] = "主公技，晋势力角色于其回合内使用第一张牌时，你可视为你使用过或未使用过此牌名的牌。",
}

local huangchao = General(extension, "lzsj__huangchao", "qi", 5)
local godhuangchao = General(extension, "lzsj__godhuangchao", "qi", 5)
godhuangchao.hidden = true
huangchao.strategy = "courage"

local lzsj__chongtian = fk.CreateTriggerSkill{
  name = "lzsj__chongtian",
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not target:insideExtraTurn() then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local end_id = turn_event.id
      local ids = {}
      local n = 0
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from ~= nil and move.from == player.id and (move.to ~= player.id or (move.toArea ~= Card.PlayerHand and move.toArea ~= Card.PlayerEquip)) then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end, end_id)
      room.logic:getActualDamageEvents(1, function(e)
        local damage = e.data[1]
        if damage.to == player then
          n = n + damage.damage
        end
      end)
      self.cost_data = {#ids, n}
      return #ids >= 2 or n > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = self.cost_data[1]
    local damage = self.cost_data[2]
    local choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    local all_choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    if n < 2 then table.removeOne(choices, all_choices[1]) end
    if damage == 0 then table.removeOne(choices, all_choices[2]) end
    local choice = player.room:askForChoice(player, choices, self.name)
    if choice == all_choices[1] then
      local card = player.room:askForDiscard(player, n, n, true, self.name, true, ".", "#lzsj__chongtian-discard:::"..n)
      return #card > 0
    elseif choice == all_choices[2] then
      player.room:loseHp(player, damage, self.name)
      return not player.dead
    end
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraTurn(true)
  end
}

local lzsj__lieshi = fk.CreateTriggerSkill{
  name = "lzsj__lieshi",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:isKongcheng()
    and (player.phase == Player.Start or player.phase == Player.Finish)
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.map(table.filter(player.room.alive_players, function (p)
      return table.every(player.room.alive_players, function (sp) return p.hp <= sp.hp end)
    end), Util.IdMapper)
    if #targets > 0 then
      local tos = player.room:askForChoosePlayers(player, targets, 1, 1, "#lzsj__lieshi-choose", self.name, false)
      if #tos > 0 then
        self.cost_data = tos
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    for _, p in ipairs(self.cost_data) do
      room:damage({
        from = player,
        to = room:getPlayerById(p),
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name,
      })
    end
  end,
}

huangchao:addSkill(lzsj__chongtian)
huangchao:addSkill(lzsj__lieshi)

local god__chongtian = fk.CreateTriggerSkill{
  name = "god__chongtian",
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local end_id = turn_event.id
      local ids = {}
      local n = 0
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from ~= nil and move.from == player.id and (move.to ~= player.id or (move.toArea ~= Card.PlayerHand and move.toArea ~= Card.PlayerEquip)) then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end, end_id)
      room.logic:getActualDamageEvents(1, function(e)
        local damage = e.data[1]
        if damage.to == player then
          n = n + damage.damage
        end
      end)
      self.cost_data = {#ids, n}
      return #ids >= 2 or n > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = self.cost_data[1]
    local damage = self.cost_data[2]
    local choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    local all_choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    if n < 2 then table.removeOne(choices, all_choices[1]) end
    if damage == 0 then table.removeOne(choices, all_choices[2]) end
    local choice = player.room:askForChoice(player, choices, self.name)
    if choice == all_choices[1] then
      local card = player.room:askForDiscard(player, n, n, true, self.name, true, ".", "#lzsj__chongtian-discard:::"..n)
      return #card > 0
    elseif choice == all_choices[2] then
      player.room:loseHp(player, damage, self.name)
      return not player.dead
    end
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraTurn(true)
  end
}

godhuangchao:addSkill(god__chongtian)
godhuangchao:addSkill(lzsj__lieshi)

Fk:loadTranslationTable{
  ["lzsj__huangchao"] = "黄巢",
  ["illustrator:lzsj__huangchao"] = "白帝",
  ["designer:lzsj__huangchao"] = "浩潇-淼龙",
  ["cv:lzsj__huangchao"] = "易大剧",
  ["lzsj__chongtian"] = "冲天",
  ["#lzsj__chongtian-discard"] = "冲天：你可弃置%arg张牌",
  [":lzsj__chongtian"] = "一名角色的非额外回合结束后，若你于此回合失去过至少两张牌或受到过伤害，你可选择弃置等量的牌或失去等量体力，然后进行一个额外的回合。",
  ["lzsj__chongtian1"] = "弃置%arg张牌",
  ["lzsj__chongtian2"] = "失去%arg点体力",
  ["lzsj__lieshi"] = "裂师",
  [":lzsj__lieshi"] = "锁定技，准备阶段或结束阶段，若你没有手牌，你摸两张牌并对场上体力值最小的一名角色造成1点伤害。",
  ["#lzsj__lieshi-choose"] = "裂师：请选择体力值最小的一名角色",

  ["lzsj__godhuangchao"] = "经典黄巢",
  ["god__chongtian"] = "冲天",
  [":god__chongtian"] = "一名角色的回合结束后，若你于此回合失去过至少两张牌或受到过伤害，你可选择弃置等量的牌或失去等量体力，然后进行一个额外的回合。",

  ["$lzsj__chongtian1"] = "冲天香阵透长安，满城尽带黄金甲！",
  ["$lzsj__chongtian2"] = "腐土越碎，越养得出冲天的野草！",
  ["$lzsj__lieshi1"] = "分兵劫敌辎重，十八路节度使互疑如仇寇！",
  ["$lzsj__lieshi2"] = "分兵不是溃逃，是让复仇的火种烧遍九州！",
  ["~lzsj__huangchao"] = "冲天香阵终散尽，谁闻菊花凋零声？",
}

local laihuer = General(extension, "lzsj__laihuer", "sui", 5)
laihuer.strategy = "courage"

local lzsj__gongguo = fk.CreateTriggerSkill{
  name = "lzsj__gongguo",
  anim_type = "offensive",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local n = ls.getAreasHasCards(player)
    local targets = table.filter(room.alive_players, function(p) return p.hp == n end)
    if #targets > 0 then
      local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#gongguo-choose", self.name, false)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
    end
  end,
  on_use = function(self, event, player, target, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local all_choices = {"lzsj__gongguo1","lzsj__gongguo2"}
    local choices = table.simpleClone(all_choices)
    if not to:isWounded() then table.removeOne(choices, "lzsj__gongguo2") end
    local choice = room:askForChoice(player, choices, self.name, nil, false, all_choices)
    if choice == "lzsj__gongguo1" then
      room:loseHp(to, 1, self.name)
    else
      ls.recover(to, 1, self.name)
    end
  end,
}

local lzsj__haolve = fk.CreateViewAsSkill{
  name = "lzsj__haolve",
  pattern = "slash,jink",
  prompt = "请选择你装备区或判定区内的一张牌",
  expand_pile = function (self, player)
    return player:getTableMark("haolve_cards")
  end,
  interaction = function (self)
    local choices = {}
    table.insertTableIfNeed(choices, Self:getCardIds("ej"))
    local all_choices = table.map(choices, function (id) return TT.getCardData(id, Fk:currentRoom()) end)
    return TT.CardNameBox { choices = choices, all_choices = all_choices}
  end,
  card_filter = function (self, to_select, selected)
    if #selected > 0 or self.interaction.data == nil then return false end
    local mark = Self:getTableMark("haolve_cards")
    local haolve_cards = table.filter(U.getAllCardNames("b"), function (name)
      return string.find(name, "lvshi__slash") or string.find(name, "jink")
    end)
    local enable_cards = U.getViewAsCardNames(Self, self.name, haolve_cards)
    local name = Fk:getCardById(to_select).name
    return table.contains(mark, to_select) and table.contains(enable_cards, name)
  end,
  view_as = function (self, cards)
    if #cards == 1 and self.interaction.data ~= nil then
      local card = Fk:cloneCard(Fk:getCardById(cards[1]).name)
      --card:addSubcard(self.interaction.data)
      card:setMark("lzsj__haolve", self.interaction.data)
      card.skillName = self.name
      return card
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) < 1
  end,
  enabled_at_response = function (self, player, response)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) < 1 and not response
  end,
  before_use = function (self, player, use)
    local room = player.room
    local id = use.card:getMark("lzsj__haolve")
    if room:getCardArea(id) == Card.PlayerEquip then
      local pattern = ""
      if Fk:getCardById(id).color == Card.Red then
        pattern = "lvshi__indulgence"
      elseif Fk:getCardById(id).color == Card.Black then
        pattern = "lvshi__supply_shortage"
      end
      ls.MoveAsDelayedTrick(id, player, pattern, self.name)
    elseif room:getCardArea(id) == Card.PlayerJudge then
      room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
    end
  end,
  on_acquire = function(self, player)
    local haolve_cards = table.filter(U.getAllCardNames("b"), function (name)
      return string.find(name, "lvshi__slash") or string.find(name, "jink")
    end)
    local mark = {player.room:printCard(haolve_cards[1]).id, player.room:printCard(haolve_cards[2]).id}
    player.room:setPlayerMark(player, "haolve_cards", mark)
  end,
}

laihuer:addSkill(lzsj__gongguo)
laihuer:addSkill(lzsj__haolve)

Fk:loadTranslationTable{
  ["lzsj__laihuer"] = "来护儿",
  ["illustrator:lzsj__huangchao"] = "",
  ["designer:lzsj__laihuer"] = "浩潇-淼龙",
  ["lzsj__gongguo"] = "拱国",
  [":lzsj__gongguo"] = "锁定技，回合开始时，你须选择一名体力值等于你有牌的区域数的角色，你令其失去1点体力或回复1点体力。",
  ["#gongguo-choose"] = "请选择一名体力值等于你有牌的区域数的角色",
  ["lzsj__gongguo1"] = "失去1点体力",
  ["lzsj__gongguo2"] = "回复1点体力",
  ["lzsj__haolve"] = "豪略",
  [":lzsj__haolve"] = "回合技，当你需要使用【杀】或【闪】时，你可将装备区内的一张牌置于判定区（红乐黑兵），或获得判定区内的一张牌，视为你使用之。",

  ["$lzsj__gongguo1"] = "铁骑镇辽东，烽火不渡辽水东！",
  ["$lzsj__gongguo2"] = "运河粮秣通，三军锋镝指平壤！",
  ["$lzsj__haolve1"] = "胡酋首级悬槊尖，百万军前祭苍天！",
  ["$lzsj__haolve2"] = "三征辽东骨作梯，大业旗扬九天上！",
  ["~lzsj__laihuer"] = "征辽三十载，终负‘护儿’名！",
}

local tuobagui = General(extension, "lzsj__tuobagui", "wei", 4)
tuobagui.strategy = "schemecourage"
tuobagui.truekingdom = "ywei"

local lzsj__saotu = fk.CreateActiveSkill{
  name = "lzsj__saotu",
  anim_type = "offensive",
  prompt = "你可以重铸手牌中所有基本牌，搜寻一张锦囊牌",
  target_num = 0,
  card_num = 0,
  can_use = Util.TrueFunc,
  card_filter = Util.FalseFunc,
  --[[card_filter = function(self, to_select)
    if not Self:prohibitDiscard(Fk:getCardById(to_select)) then
      return Fk:getCardById(to_select).type == Card.TypeBasic
    end
  end,]]
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = table.filter(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeBasic end)
    if #cards == 0 then room:addTableMark(player, MarkEnum.InvalidSkills .. "-phase", self.name) return end
    room:recastCard(cards, player, self.name)
    if player:isAlive() then
      local ids = {}
      local card = {}
      while true do
        local id = room:getNCards(1, "bottom")[1]
        room:moveCardTo(id, Card.Processing, nil, fk.ReasonJustMove, "searchcard")
        room:delay(200)
        local c = Fk:getCardById(id)
        if c.type == Card.TypeTrick then
          room:setCardEmotion(id, "Judgegood")
          table.insert(card, id)
          break
        else
          table.insert(ids, id)
        end
      end
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
      if #ids > 0 then
        room:moveCardTo(ids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "searchcard")
      end
      if #table.filter(ids, function (id) return Fk:getCardById(id).type == Card.TypeBasic end) <= #cards then
        room:delay(300)
        --room:recastCard(card, player, self.name)
        room:throwCard(card, self.name, player, player)
        local range = player:getMark("@saotu_range-phase")
        local targets = room.alive_players
        if player:getMark("saotu_damage-phase") > 0 then
          targets = table.filter(room.alive_players, function (p)
            return p:compareDistance(player, player:getMark("@saotu_range-phase"), ">")
          end)
        end
        if #targets == 0 then
          room:addTableMark(player, MarkEnum.InvalidSkills .. "-phase", self.name)
        else
          local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#saotu_damage", self.name, false)
          if #tos > 0 then
            room:addPlayerMark(player, "saotu_damage-phase")
            room:setPlayerMark(player, "@saotu_range-phase", room:getPlayerById(tos[1]):distanceTo(player))
            room:damage({
              from = player,
              to = room:getPlayerById(tos[1]),
              damage = 1,
              damageType = fk.NormalDamage,
              skillName = self.name,
            })
          end
        end
      end
    end
  end,
}

tuobagui:addSkill(lzsj__saotu)

local lzsj__guji = fk.CreateTriggerSkill{
  name = "lzsj__guji$",
  frequency = Skill.Compulsory,
  mute = true,
  events = {fk.GameStart, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and target == player and player.phase == Player.Play
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      player.maxHp = player.maxHp + 1
      player.hp = player.hp + 1
      room:broadcastProperty(player, "maxHp")
      room:broadcastProperty(player, "hp")
      room:notifySkillInvoked(player, self.name)
      player:broadcastSkillInvoke(self.name)
    else
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if Fk.generals[p.general].truekingdom == "ywei" then
          local cards = room:askForCard(p, 1, 1, true, self.name, true, ".", "#lzsj__guji-invoke::"..player.id, self.name)
          if #cards > 0 then
            local choices = {"give"}
            local all_choices = {"give", "use"}
            local card = Fk:getCardById(cards[1])
            local canUse = p:canUseTo(card, player, {bypass_distances = true, bypass_times = true}) and not
            (card.skill:getMinTargetNum() == 0 and not card.multiple_targets)
            if canUse then table.insert(choices, "use") end
            local choice = room:askForChoice(player, choices, self.name, nil, false, all_choices)
            if choice == "use" then
              local use = {
                from = p.id,
                tos = {{player.id}},
                card = card,
                extraUse = true,
              }
              use.extra_data = use.extra_data or {}
              room:useCard(use)
              if player:isAlive() and p:isAlive() and not p:isNude() then
                local id = room:askForCardChosen(player, p, "he", self.name)
                room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
              end
            else
              room:moveCardTo(cards, Player.Hand, player, fk.ReasonPrey, self.name, nil, true, player.id)
            end  
          end
        end
      end
    end
  end,
}

tuobagui:addSkill(lzsj__guji)

Fk:loadTranslationTable{
  ["lzsj__tuobagui"] = "拓跋珪",
  ["#lzsj__tuobagui"] = "朔寒元启",
  ["illustrator:lzsj__tuobagui"] = "白飞浪",
  ["designer:lzsj__tuobagui"] = "Erosa",
  ["lzsj__saotu"] = "扫途",
  ["@saotu_range-phase"] = "扫途",
  ["#saotu_damage"] = "扫途：请选择受到伤害的角色",
  [":lzsj__saotu"] = "出牌阶段，你可重铸手牌中所有基本牌，搜寻一张锦囊牌（从牌堆底），若以此法亮出的基本牌数量不大于重铸牌数，你须弃置此锦囊牌，然后对一名与你距离比此阶段上次发动更远的角色造成1点伤害，若无角色可选择，你于此阶段不能再发动此技能。",
  ["lzsj__guji"] = "孤极",
  [":lzsj__guji"] = "主公技，你的体力上限与体力值再+1；出牌阶段开始时，其他元魏角色依次可选择：交给你一张牌，或对你使用一张牌，然后你弃置其一张牌。<br/>（目标需合法，不能使用桃或装备牌等）",
  ["#lzsj__guji-invoke"] = "孤极：请选择一张牌交给%dest或对%dest使用（目标需合法）",

  ["$lzsj__saotu1"] = "铁蹄碾碎贺兰雪，敕勒川尽归魏土！",
  ["$lzsj__saotu2"] = "马踏慕容旗，刀劈柔然帐！",
  ["$lzsj__guji1"] = "盛乐宫深，唯孤刃可斩群狼！",
  ["$lzsj__guji2"] = "寒刃指北，谁敢不随孤极星！",
  ["~lzsj__tuobagui"] = "饮尽塞北霜，终冻枭雄骨...",
}


local sujun = General(extension, "lzsj__sujun", "jin", 5)
sujun.strategy = "courage"

local lzsj__beishi = fk.CreateActiveSkill{
  name = "lzsj__beishi",
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, _, _, _, player)
    return to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local players = {}
    for _, p in ipairs(effect.tos) do
      local fire = Fk:cloneCard("lvshi__fire_attack")
      fire.skillName = self.name
      if room:getPlayerById(p):canUseTo(fire, player) then
        local use = {
          from = p,
          tos = {{ player.id }},
          card = fire,
          extraUse = true,
        }
        room:useCard(use)
        if player.dead then return end
        if not use.damageDealt then
          table.insert(players, p)
        end
      end
    end
    if not player.dead then
      for _, p in ipairs(effect.tos) do
        if not room:getPlayerById(p).dead then
          local cards = room:askForCard(player, 1, 1, false, self.name, true, tostring(Exppattern{ id = DIY.GetShownCards(player) }), "#lzsj__beishi-use:"..p)
          if #cards > 0 then
            local unexpectation = Fk:cloneCard("unexpectation")
            unexpectation:addSubcard(cards[1])
            local use = {
              from = player.id,
              tos = {{ p }},
              card = unexpectation,
              additionalDamage = #table.filter(players, function (id) return p == id end),
            }
            room:useCard(use)
            --room:useVirtualCard("unexpectation", cards, player, room:getPlayerById(p), self.name, true)
            if player.dead then return end 
          end
        end
      end
    end
  end,
}

local lzsj__zhitu = fk.CreateTriggerSkill{
  name = "lzsj__zhitu",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      local room = player.room
      if room:getBanner("@$CenterArea") == nil then return end
      local ids = room:getBanner("@$CenterArea")
      local reds = table.filter(ids, function (id) return Fk:getCardById(id).color == Card.Red end)
      return #reds > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local phases = {"judge", "draw", "play", "discard"}
    local ids = room:getBanner("@$CenterArea")
    local reds = table.filter(ids, function (id) return Fk:getCardById(id).color == Card.Red end)
    local mark = {}
    local n = math.min(#reds, 4)
    for i = 1, n do
      table.insert(mark, phases[i])
    end
    player.room:setPlayerMark(player, "lzsj__zhitu", mark)
    if table.contains(mark, "draw") then
      room:loseHp(player, 1, self.name)
    end
    if not player.dead then
      player:gainAnExtraTurn(true)
    end
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:getMark("lzsj__zhitu") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("lzsj__zhitu")
    room:setPlayerMark(player, "lzsj__zhitu", 0)
    if not table.contains(mark, "draw") then
      player:skip(Player.Draw)
    end
    if table.contains(mark, "play") then
      room:addTableMark(player, MarkEnum.InvalidSkills .. "-round", self.name)
    else
      player:skip(Player.Play)
    end
    if not table.contains(mark, "discard") then
      player:skip(Player.Discard)
    end
  end,
}

lzsj__zhitu.CenterArea = true
sujun:addSkill(lzsj__beishi)
sujun:addSkill(lzsj__zhitu)

Fk:loadTranslationTable{
  ["lzsj__sujun"] = "苏峻",
  ["#lzsj__sujun"] = "巍寒着焰",
  ["illustrator:lzsj__sujun"] = "白帝",
  ["designer:lzsj__sujun"] = "淼龙",
  ["cv:lzsj__sujun"] = "易大剧",
  ["lzsj__beishi"] = "悖噬",
  [":lzsj__beishi"] = "阶段技，出牌阶段，你可令任意名角色依次视为对你使用【火攻】，然后你依次选择是否将一张明置牌当【出其不意】对每名你选择的角色使用，此【出其不意】对未因此【火攻】对你造成伤害的角色造成的伤害+1。",
  ["#lzsj__beishi-use"] = "你可将一张明置牌当【出其不意】对%src使用",
  
  ["lzsj__zhitu"] = "炙途",
  [":lzsj__zhitu"] = "锁定技，回合结束时，你执行一个仅有前X个<a href='main_phase'>主要阶段</a>的额外回合（X为中央区红色牌数），若此回合含有：摸牌阶段，你失去1点体力；出牌阶段，此技能于此轮失效。",

  ["main_phase"] = "判定阶段，摸牌阶段，出牌阶段，弃牌阶段",
  ["$lzsj__beishi1"] = "既挟震主威！何惧鼎镬沸！",
  ["$lzsj__beishi2"] = "王马共天下？獠牙断司马！",
  ["$lzsj__zhitu1"] = "台省付焦炭！玉牒煅新玺！",
  ["$lzsj__zhitu2"] = "建康燎天火，正为烹龙炊！",
  ["~lzsj__sujun"] = "昔啖胡羯肉，今作...庾氏脍...",
}

local yuyi = General(extension, "lzsj__yuyi", "jin", 4)
yuyi.strategy = "courage"

local lzsj__shedi = fk.CreateActiveSkill{
  name = "lzsj__shedi",
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  prompt = "#lzsj__shedi",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return #player:getTableMark("shedi-phase") < 2
  end,
  interaction = function(self, player)
    local all_choices = {"lzsj__shedi1", "lzsj__shedi2"}
    local choices = {"lzsj__shedi1", "lzsj__shedi2"}
    for _, n in ipairs(all_choices) do
      if table.contains(player:getTableMark("shedi-phase"), n) then
        table.removeOne(choices, n)
      end
    end
    if #choices > 0 then
      return UI.ComboBox { choices = choices }
    end
  end,
  on_use = function(self, room, effect)
    if not self.interaction.data then return end
    local player = room:getPlayerById(effect.from)
    local mark = player:getTableMark("shedi-phase")
    table.insertIfNeed(mark, self.interaction.data)
    room:setPlayerMark(player, "shedi-phase", mark)
    local range = player:getAttackRange()
    local players = table.filter(room.alive_players, function (p) return player:inMyAttackRange(p) end)
    if self.interaction.data == "lzsj__shedi1" then
      room:setPlayerMark(player, "@shedi_attackrange-turn", player:getHandcardNum())
      TT.adjustHandNum(player, range, self.name)
    else
      room:setPlayerMark(player, "@shedi_attackrange-turn", player:getMaxCards())
      room:setPlayerMark(player, "shedi_maxcards-turn", range)
    end
    if player:getAttackRange() > range then
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#lzsj__shedi-damage", self.name, false)
      if #tos > 0 then
        room:damage { from = player, to = room:getPlayerById(tos[1]), damage = 1, skillName = self.name }
      end
    elseif player:getAttackRange() < range then
      local targets = table.filter(room.alive_players, function (p) return player:inMyAttackRange(p) end)
      targets = table.filter(targets, function (p) return not table.contains(players, p) end)
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 2, "#lzsj__shedi-draw", self.name, true)
      if #tos > 0 then
        for _, p in ipairs(tos) do
          room:getPlayerById(p):drawCards(1, self.name)
        end
      end
    end
  end,
}

local shedi_attackrange = fk.CreateAttackRangeSkill{
  name = "#lzsj__shedi_attackrange",
  fixed_func = function (self, from)
    if from:hasSkill(self) and #from:getTableMark("shedi-phase") ~= 0 then
      return from:getMark("@shedi_attackrange-turn")
    end
  end,
}

local shedi_maxcards = fk.CreateMaxCardsSkill{
  name = "#lzsj__shedi_maxcards",
  fixed_func = function(self, player)
    if player:getMark("shedi_maxcards-turn") > 0 then
      return player:getMark("shedi_maxcards-turn")
    end
  end
}

lzsj__shedi:addRelatedSkill(shedi_attackrange)
lzsj__shedi:addRelatedSkill(shedi_maxcards)
yuyi:addSkill(lzsj__shedi)

Fk:loadTranslationTable{
  ["lzsj__yuyi"] = "庾翼",
  ["#lzsj__yuyi"] = "执纲振羽",
  ["illustrator:lzsj__yuyi"] = "白帝",
  ["designer:lzsj__yuyi"] = "Erosa",
  ["cv:lzsj__yuyi"] = "Erosa",
  ["lzsj__shedi"] = "射的",
  ["lzsj__shedi1"] = "手牌数",
  ["lzsj__shedi2"] = "手牌上限",
  ["#lzsj__shedi"] = "请选择手牌数与手牌上限中此阶段未选择过的一项",
  [":lzsj__shedi"] = "出牌阶段，你可用手牌数与手牌上限中此阶段未选择过的一项与攻击范围交换：若你的攻击范围增大，你可分配1点伤害；若你的攻击范围减小，你可以令离开攻击范围的至多两名角色各摸一张牌。<br/>（攻击范围取和武器的攻击范围最大值）",
  ["lzsj__shedi1"] = "手牌数",
  ["lzsj__shedi2"] = "手牌上限",
  ["@shedi_attackrange-turn"] = "攻击范围",
  ["#lzsj__shedi-damage"] = "你可以分配1点伤害",
  ["#lzsj__shedi-draw"] = "你可以令至多两名角色各摸一张牌",

  ["$lzsj__shedi1"] = "箭指中原，志在克复！",
  ["$lzsj__shedi2"] = "整军经武，岂容胡马南渡！",
  ["~lzsj__yuyi"] = "北伐未竟，九泉...难瞑目...",
}

local huanwen = General(extension, "lzsj__huanwen", "jin", 4)
huanwen.strategy = "schemecourage"

local lzsj__zaifa = fk.CreateTriggerSkill{
  name = "lzsj__zaifa",
  events = {fk.CardUseFinished},
  anim_type = "drawCards",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.skillName ~= self.name
    and data.card.type ~= Card.TypeEquip and data.card.sub_type ~= Card.SubtypeDelayedTrick
    and data.card.trueName ~= "jink" and data.card.trueName ~= "nullification"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:isKongcheng() then
      room:useVirtualCard("reinforcement", nil, player, player, self.name, true)
      room:addTableMark(player, MarkEnum.InvalidSkills .. "-turn", self.name)
    else
      player:showCards(player:getCardIds("h"))
      if table.every(player:getCardIds("h"), function (id) return Fk:getCardById(id).suit ~= data.card.suit end) then
        local success, dat = player.room:askForUseActiveSkill(player, "lzsj__zaifa_viewas", "#lzsj__zaifa-use:::"..data.card:toLogString(),
        true, {vs_name = data.card.name, bypass_times = true})
        if success then
          local card = Fk:cloneCard(data.card.name)
          card.skillName = self.name
          card:addSubcard(dat.cards[1])
          player.room:useCard{
            from = player.id,
            tos = table.map(dat.targets, function(id) return {id} end),
            card = card,
            extraUse = true,
          }
        end   
      end
    end
  end,
}

local zaifa_viewas = fk.CreateViewAsSkill{
  name = "lzsj__zaifa_viewas",
  pattern = ".",
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(self.vs_name)
    card:addSubcards(cards)
    card.skillName = "lzsj__zaifa"
    return card
  end,
}

local lzsj__qingqi = fk.CreateTriggerSkill{
  name = "lzsj__qingqi",
  events = {fk.TurnEnd},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      return ls.countSuits(player:getCardIds("h")) > player:getMark("@lzsj__qingqi")
    end
  end,
  on_cost = function(self, event, target, player, data)
    local targets = player.room:askForChooseToMoveCardInBoard(player, "#lzsj__qingqi-move", self.name, true, nil)
    if #targets ~= 0 then
      targets = table.map(targets, function(p) return player.room:getPlayerById(p) end)
      self.cost_data = targets
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local cards = ls.gethidecards(player)
    player.room:setPlayerMark(player, "@lzsj__qingqi", ls.countSuits(cards))
    DIY.ShowCards(player, cards)
    player.room:askForMoveCardInBoard(player, self.cost_data[1], self.cost_data[2], self.name)
  end,

  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.to and move.to == player.id and move.toArea == Card.PlayerEquip then
          for _, info in ipairs(move.moveInfo) do
            return Fk:getCardById(info.cardId).sub_type == Card.SubtypeTreasure
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@lzsj__qingqi", 0)
  end,
}

Fk:addSkill(zaifa_viewas)
huanwen:addSkill(lzsj__zaifa)
huanwen:addSkill(lzsj__qingqi)

Fk:loadTranslationTable{
  ["lzsj__huanwen"] = "桓温",
  ["#lzsj__huanwen"] = "英质殇雄",
  ["illustrator:lzsj__huanwen"] = "伊达未来",
  ["designer:lzsj__huanwen"] = "Erosa",
  ["cv:lzsj__huanwen"] = "Erosa",
  ["lzsj__zaifa"] = "再伐",
  [":lzsj__zaifa"] = "你不因“再伐”使用即时类牌后，若你有手牌，你可展示所有手牌，若不含此牌花色，你可将一张牌当此牌使用；若你没有手牌，你可视为对自己使用【增兵减灶】，然后此回合“再伐”无效。",
  ["#lzsj__zaifa-use"] = "再伐：你可将一张牌当%arg使用",
  ["@lzsj__qingqi"] = "再伐",
  ["lzsj__qingqi"] = "倾器",
  [":lzsj__qingqi"] = "回合结束时，若你手牌的花色数大于X，你可明置所有手牌来移动场上一张牌，当宝物牌进入你的装备区后，将X置0。（X为你上次发动“倾器”明置的花色数）",
  ["#lzsj__qingqi-move"] = "请选择两名角色，你明置所有手牌并在这些角色间移动一张牌",
  ["$lzsj__zaifa1"] = "三秦父老箪食迎，岂可空负官军名！",
  ["$lzsj__zaifa2"] = "灞水秋风卷铁甲，不破长安不书凯！",
  ["$lzsj__qingqi1"] = "若不能流芳百世，遗臭万年又何妨？",
  ["$lzsj__qingqi2"] = "废立由心非僭越，江东需我定乾坤！",
  ["~lzsj__huanwen"] = "木犹如此，人何以堪…",
}

local xieai = General(extension, "lzsj__xieai", "jliang", 3)
xieai.strategy = "scheme"
xieai.guo16 = true

local lzsj__woding = fk.CreateTriggerSkill{
  name = "lzsj__woding",
  events = {fk.TurnStart},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player-- and #U.getPrivateMark(player, "$woding") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = #table.filter(player:getCardIds("h"), function (id)
      return table.contains(U.getPrivateMark(player, "$woding"), id)
    end)
    local m = player:getHandcardNum() - n
    player:drawCards(n)
    if m >= 2 then
      m = math.floor(m / 2)
      for i = 1, m do
        local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#lzsj__woding-invoke", self.name, false)
        if #tos > 0 then
          room:damage { from = player, to = room:getPlayerById(tos[1]), damage = 1, skillName = self.name }
        end
      end
    end
  end,

  refresh_events = {fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    if player:isKongcheng() then
      player.room:setPlayerMark(player, "@[private]$woding", 0)
    else
      U.setPrivateMark(player, "$woding", player:getCardIds("h"))
    end
  end,
}

xieai:addSkill(lzsj__woding)

local lzsj__yizhen = fk.CreateTriggerSkill{
  name = "lzsj__yizhen",
  events = {fk.TurnStart, fk.TurnEnd},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(self) and target:inMyAttackRange(player) and player:getHandcardNum() < 4
    else
      return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player.room:askForSkillInvoke(player, self.name)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TurnStart then
      local n = 4 - player:getHandcardNum()
      local cards = player:drawCards(n, self.name)
      table.forEach(cards, function (id)
        player.room:addCardMark(Fk:getCardById(id), "@@yizhen-turn")
      end)
      player.room:setPlayerMark(player, "yizhen-turn", cards)
    else
      local choice = player.room:askForChoice(player, {"lzsj__yizhen1", "lzsj__yizhen2"}, self.name)
      local cards
      if choice == "lzsj__yizhen1" then
        cards = table.filter(player:getCardIds("h"), function (id)
          return table.contains(player:getTableMark("yizhen-turn"), id)
        end)
      else
        cards = table.filter(player:getCardIds("h"), function (id)
          return not table.contains(player:getTableMark("yizhen-turn"), id)
        end)
      end
      if #cards > 0 then
        player.room:throwCard(cards, self.name, player, player)
      end
    end
  end,
}

xieai:addSkill(lzsj__yizhen)

Fk:loadTranslationTable{
  ["lzsj__xieai"] = "谢艾",
  ["#lzsj__xieai"] = "轻羽成疆",
  ["illustrator:lzsj__xieai"] = "",
  ["designer:lzsj__xieai"] = "lapras",
  ["cv:lzsj__xieai"] = "Erosa",
  ["lzsj__woding"] = "卧定",
  [":lzsj__woding"] = "回合开始时，你每有一张手牌与上回合结束时相同，则你摸一张牌；你每有两张手牌与上回合结束时不同，你分配1点伤害。",
  ["@[private]$woding"] = "卧定",
  ["#lzsj__woding-invoke"] = "卧定：请分配1点伤害",
  ["lzsj__yizhen"] = "易阵",
  [":lzsj__yizhen"] = "其他角色的回合开始时，若你在其攻击范围内，你可将手牌补至四张；此回合结束时，你选择弃置以此法摸的牌或其余手牌。",
  ["@@yizhen-turn"] = "易阵",
  ["lzsj__yizhen1"] = "弃置易阵牌",
  ["lzsj__yizhen2"] = "弃置其余牌",

  ["$lzsj__woding1"] = "枭鸣振武营，此战必胜之兆！",
  ["$lzsj__woding2"] = "白帽轻车临敌阵，胡床稳坐定军心。",
  ["$lzsj__yizhen1"] = "阵前鸣鼓惑敌目，阵后奇兵破中军！",
  ["$lzsj__yizhen2"] = "张瑁已断敌军后，此时不击待何时？",
  ["~lzsj__xieai"] = "张祚篡诏诛忠骨，凉州长城自此倾...",
}


local yuanxiu = General(extension, "lzsj__yuanxiu", "wei", 3, 4)
yuanxiu.strategy = "scheme"
yuanxiu.truekingdom = "ywei"

local wangfeng = fk.CreateActiveSkill{
  name = "lzsj__wangfeng",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lzsj__wangfeng",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    if #selected > 0 then return false end
    return to_select ~= player.id and #Fk:currentRoom():getPlayerById(to_select):getCardIds("he") > 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local cards = room:askForCard(to, 2, 2, true, self.name, false, ".", "#lzsj__wangfeng-give::"..effect.from)
    if #cards > 0 then
      room:obtainCard(player, cards, true, fk.ReasonGive, player.id, self.name)
    end
    if room:askForSkillInvoke(to, self.name, nil, "#wangfeng-jushou:"..player.id) then
      room:notifySkillInvoked(player, "lzsj__jushou", "drawcard")
      player:broadcastSkillInvoke("lzsj__jushou")
      player:turnOver()
      room:drawCards(player, 4, "lzsj__jushou")
      local jushou_card, throw_card
      for _, id in pairs(player:getCardIds(Player.Hand)) do
        local card = Fk:getCardById(id)
        if (card.type == Card.TypeEquip and not player:prohibitUse(card)) or (card.type ~= Card.TypeEquip and not player:prohibitDiscard(card)) then
          jushou_card = card
          break
        end
      end
      --if not jushou_card then return end
      local _, ret = room:askForUseActiveSkill(player, "#plum__jushou_select", "#plum__jushou-select", false)
      if ret then
        jushou_card = Fk:getCardById(ret.cards[1])
      end
      if jushou_card then
        if jushou_card.type == Card.TypeEquip then
          room:useCard({
            from = player.id,
            tos = {{player.id}},
            card = jushou_card,
          })
        else
          throw_card = jushou_card:getEffectiveId()
          room:throwCard(throw_card, self.name, player, player)
        end
      end
      if player.phase == Player.Play then
        player._phase_end = true
      end
      room:endTurn()
    else
      if not to.dead then
        to:control(player)
        room:handleAddLoseSkills(player, "lszj__jizhi", nil, false, false)
        for i = 1, 2 do
          if not U.askForPlayCard(room, player, player:getHandlyIds(true), ".", self.name) then
            break
          end
        end
        room:handleAddLoseSkills(player, "-lszj__jizhi", nil, false, false)
        player:control(player)
      end
    end
  end,
}


local zhuchong = fk.CreateTriggerSkill {
  name = "lzsj__zhuchong",
  frequency = Skill.Limited,
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player ~= target and target.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
    and not player.faceup
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local jinks = table.filter(player:getCardIds("h"), function (id) return Fk:getCardById(id).trueName == "jink" end)
    if #jinks > 0 then
      room:recastCard(jinks, player, self.name)
    end
    target:endPlayPhase()
    room:setPlayerMark(player, "@zhuchong", #jinks)
    room:addPlayerMark(player, "zhuchong")
    player:gainAnExtraPhase(Player.Play, true)
  end,

  refresh_events = {fk.EventPhaseEnd},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
    and player:getMark("zhuchong") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "zhuchong", 0)
    if #room.logic:getEventsOfScope(GameEvent.ChangeHp, 999, function (e)
      local damage = e.data[5]
      if damage and player == damage.from then
        return true
      end
    end, Player.HistoryPhase) > player:getMark("@zhuchong") then
      player:setSkillUseHistory(self.name, 0, Player.HistoryGame)
    else
      local n = player.maxHp - player.hp
      room:changeMaxHp(player, -n)
    end
    room:setPlayerMark(player, "@zhuchong", 0)
  end,
}

yuanxiu:addSkill(wangfeng)
yuanxiu:addSkill(zhuchong)

local lszj__jizhi = fk.CreateTriggerSkill{
  name = "lszj__jizhi",
  anim_type = "drawcard",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeTrick and
    (not data.card:isVirtual() or #data.card.subcards == 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:drawCards(1)
    if #cards == 0 then return false end
    local card = Fk:getCardById(cards[1])
    if card.type == Card.TypeBasic and not player.dead and player.phase ~= Player.NotActive and
    table.contains(player:getCardIds("h"), card.id) and not player:prohibitDiscard(card) and
    room:askForSkillInvoke(player, self.name, nil, "#jizhi-invoke:::"..card:toLogString()) then
      room:throwCard(card.id, self.name, player, player)
      room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn)
    end
  end,
}

Fk:addSkill(lszj__jizhi)
yuanxiu:addRelatedSkill(lszj__jizhi)
yuanxiu:addRelatedSkill("lzsj__jushou")

Fk:loadTranslationTable{
  ["lzsj__yuanxiu"] = "元修",
  ["#lzsj__yuanxiu"] = "恣鳞辜作",
  ["cv:lzsj__yuanxiu"] = "李嘉图Ricardo",
  ["designer:lzsj__yuanxiu"] = "拉普拉斯",
  ["lzsj__wangfeng"] = "枉奉",
  [":lzsj__wangfeng"] = "阶段技，你可令一名角色交给你两张牌，然后其选择一项：其观看你手牌并控制你使用至多两张牌，且结算中你视为有“集智”；或你发动“据守”，且结束此回合。",
  ["#lzsj__wangfeng"] = "你可令一名角色交给你两张牌",
  ["#lzsj__wangfeng-give"] = "请选择交给%dest的牌",
  ["#wangfeng-jushou"] = "枉奉：你可令%src发动【据守】，结束此回合",

  ["lzsj__zhuchong"] = "逐冲",
  [":lzsj__zhuchong"] = "限定技，其他角色的出牌阶段开始时，若你背面向上，你可重铸所有【闪】，<a href='replace_phase'>代替其执行此阶段</a>，若你于此阶段造成的伤害大于X，重置此技能；否则，你失去体力上限到体力值（X为重铸【闪】的数量）。",
  ['replace_phase'] = "其结束此阶段，你额外执行一个此阶段",
  ["@zhuchong"] = "逐冲",
  ["lszj__jizhi"] = "集智",
  [":lszj__jizhi"] = "你使用锦囊牌时，可摸一张牌，若为基本牌，你可弃置此牌令此回合手牌上限+1。",
  ["lzsj__jushou"] = "据守",
  [":lzsj__jushou"] = "回合结束时，你可摸四张牌并翻面，然后选择使用一张装备牌或弃置一张非装备牌。",

  ["$lzsj__wangfeng1"] = "丹书化烬随风散，辰光黯照三垒寒！",
  ["$lzsj__wangfeng2"] = "辰星寥落剑鸣哑，空对八柱国印痕！",
  ["$lzsj__zhuchong1"] = "曙色未明别洛邑，单骑踏碎五更霜！",
  ["$lzsj__zhuchong2"] = "逐鹿须借关中铁，冲冠怒焚邺宫帛！",
  ["$lszj__jizhi"] = "星聚关陇谋易帜，借刀贺兰斩邺枭！",
  ["$lzsj__jushou"] = "渭水冰封锁龙舟，空握玉圭听更漏！",
  ["~lzsj__yuanxiu"] = "曙星黯堕武关道，黑獭帐中鸩羽寒。",
}

return extension
